<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="700"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="所在地">
          <a-select v-decorator="['location', {
            rules: [
              {
                required: true,
                message: '所在地不能为空!',
              },
            ],
            }]" placeholder="请选择所在地" @change="handleChange">
            <a-select-option v-for="(i,index) in locationList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="项目名称">
          <a-input v-decorator="['entryName', {
           rules: [
              {
                required: true,
               message: '项目名称不能为空！',
              },
            ],
            }]" placeholder="请输入项目名称"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="项目状态">
          <a-select v-decorator="['projectStatus', {
            rules: [
              {
                required: true,
                message: '项目状态不能为空!',
              },
            ],
            }]" placeholder="请选择项目状态" @change="handleChange">
            <a-select-option v-for="(i,index) in statusList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开工时间">
          <a-input v-decorator="['commencementTime', {
           rules: [
              {
                required: true,
               message: '开工时间不能为空！',
              },
            ],
            }]" placeholder="请输入开工时间"/>
<!--          <a-date-picker-->
<!--            v-decorator="['commencementTime', {initialValue: currentTime, rules: [{ required: true, message: '请选择创建时间!' }] }]"-->
<!--            format="yyyy-MM-DD"-->
<!--            placeholder="请选择开工时间"-->
<!--            showTime-->
<!--            style="width: 100%"-->
<!--          />-->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="投产时间">
          <a-input v-decorator="['productionTime', {
           rules: [
              {
                required: true,
               message: '投产时间不能为空！',
              },
            ],
            }]" placeholder="请输入投产时间"/>
<!--          <a-date-picker-->
<!--            v-decorator="['productionTime', {initialValue: currentTime, rules: [{ required: true, message: '请选择投产时间!' }] }]"-->
<!--            format="yyyy-MM-DD"-->
<!--            placeholder="请选择投产时间"-->
<!--            showTime-->
<!--            style="width: 100%"-->
<!--          />-->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="总投资">
          <a-input v-decorator="['totalInvestment', {
           rules: [
              {
                required: true,
               message: '总投资不能为空！',
              },
            ],
            }]" placeholder="请输入总投资金额" suffix="亿元"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="完成投资">
          <a-input v-decorator="['completionInvestment', {
           // rules: [
           //    {
           //      required: true,
           //     message: '完成投资不能为空！',
           //    },
           //  ],
            }]" placeholder="请输入完成投资金额" suffix="亿元"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设计能耗">
          <a-input v-decorator="['designEnergyConsumption', {
           rules: [
              {
                required: true,
               message: '设计能耗不能为空！',
              },
            ],
            }]" placeholder="请输入设计能耗"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="“十四五”新增能耗">
          <a-input v-decorator="['newEnergyConsumption', {
           rules: [
              {
                required: true,
               message: '“十四五”新增能耗不能为空！',
              },
            ],
            }]" placeholder="请输入“十四五”新增能耗"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { httpAction, postAction } from '@api/manage'
import moment from 'moment'
// const date = new Date()
export default {
  name: 'dblHighModal',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      currentTime: '',
      dialogStyle: {
        top: '100px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },statusList: [
        { name: '拟建', key: '拟建' },
        { name: '在建', key: '在建' },
      ],
      locationList: [
        { name: '上海市化工区', key: '上海市化工区' },
        { name: '上海市金山区', key: '上海市金山区' },
        { name: '上海市宝山区', key: '上海市宝山区' },
        { name: '上海市奉贤区', key: '上海市奉贤区' },
        { name: '上海市闵行区', key: '上海市闵行区' },
        { name: '临港管委会', key: '临港管委会' },
        { name: '上海市浦东新区', key: '上海市浦东新区' },
      ],
      url: {
        add: '/carbon/twoHighProjects/add',
        edit: '/carbon/twoHighProjects/edit'
      }
    }
  },
  created() {
    this.getCurrentData()
  },
  methods: {
    getCurrentData() {
      if (this.currentTime === '') {
        this.currentTime = moment().format('YYYY-MM-DD')
      }
    },
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'location',
          'entryName',
          'projectStatus',
          'commencementTime',
          'productionTime',
          'totalInvestment',
          'completionInvestment',
          'designEnergyConsumption',
          'newEnergyConsumption',
        ))
      })
    },
    handleChange(value) {
      console.log(value)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          httpAction(httpurl, formData, method).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('refresh')
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
            that.$parent.$parent.loadData()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>